package org.objective.base;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.objective.service.Loader;

/**
 *
 * @author gus
 */
public class ViewCollection<T extends BaseView, TOwner extends BaseObject> implements ObjectCollection<T, TOwner>
{
    private final TOwner owner;
    private final List<T> listViews = new ArrayList<T>(3);
    private final Construction<T> ctor;

    public ViewCollection(TOwner owner, Construction<T> ctor)
    {
        this.owner = owner;
        this.ctor = ctor;
    }
    
    @Override
    public int size()
    {
        return listViews.size();
    }

    @Override
    public boolean isEmpty()
    {
        return listViews.isEmpty();
    }

    @Override
    public T get(int id)
    {
        return listViews.get(id);
    }

    @Override
    public T get(String name)
    {
        for (T field : listViews)
        {
            if (field.getName().equalsIgnoreCase(name))
            {
                return field;
            }
        }
        return null;
    }

    @Override
    public T newInstance() throws Exception
    {
        return newInstance(ctor);
    }

    @Override
    public T newInstance(Construction constructionCallback) throws Exception
    {
        return newInstance(ctor);
    }

    @Override
    public void load(Loader loader, long[] fieldIdList) throws Exception
    {
        owner.storage.getObjects(fieldIdList);
    }

    @Override
    public T[] toArray()
    {
        return (T[]) listViews.toArray();
    }

    @Override
    public T[] toArray(ObjectFilter<T> filter)
    {
        return (T[]) listViews.toArray();
    }

    @Override
    public Iterator<T> iterator()
    {
        return listViews.iterator();
    }
}
